package org.speed.mes.base.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.speed.mes.base.domain.bo.BsProcessBo;
import org.speed.mes.base.domain.vo.BsProcessVo;
import org.speed.mes.base.domain.BsProcess;
import org.speed.mes.base.mapper.BsProcessMapper;
import org.speed.mes.base.service.IBsProcessService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 工序管理Service业务层处理
 *
 * @author speed-mes
 * @date 2024-06-19
 */
@RequiredArgsConstructor
@Service
public class BsProcessServiceImpl implements IBsProcessService {

    private final BsProcessMapper baseMapper;

    /**
     * 查询工序管理
     */
    @Override
    public BsProcessVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询工序管理列表
     */
    @Override
    public TableDataInfo<BsProcessVo> queryPageList(BsProcessBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BsProcess> lqw = buildQueryWrapper(bo);
        Page<BsProcessVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工序管理列表
     */
    @Override
    public List<BsProcessVo> queryList(BsProcessBo bo) {
        LambdaQueryWrapper<BsProcess> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BsProcess> buildQueryWrapper(BsProcessBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BsProcess> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), BsProcess::getCode, bo.getCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), BsProcess::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), BsProcess::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增工序管理
     */
    @Override
    public Boolean insertByBo(BsProcessBo bo) {
        BsProcess add = MapstructUtils.convert(bo, BsProcess.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改工序管理
     */
    @Override
    public Boolean updateByBo(BsProcessBo bo) {
        BsProcess update = MapstructUtils.convert(bo, BsProcess.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BsProcess entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除工序管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
